package zju.ccnt.rest.domain;

import org.springframework.security.oauth2.common.util.SerializationUtils;
import zju.ccnt.rest.api.ApiCreateRankList;
import zju.ccnt.rest.service.util.ServiceUtil;
import zju.ccnt.rest.service.util.constant.RankListType;
import zju.ccnt.rest.service.util.sql.SqlPersistence;

import javax.persistence.*;
import java.sql.Timestamp;

/**
 * Created by zha on 14/11/20.
 */
@Entity
public class RankList extends BaseEntity {
    private String businessId;
    private String name;
    private String description;
    private boolean isPublic;
    private String viewName;
    private Timestamp updateTime;
//    @Lob
//    private byte[] sql;
    @Lob
    private SqlPersistence sqlQuery;

    @Enumerated(EnumType.STRING)
    private RankListType type;
    private int weight;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "businessId", insertable = false, updatable = false)
    private BusinessUser businessUser;

    public RankList(){super();}

    public RankList(ApiCreateRankList apiCreateRankList, String ownerEmail){
        super();
        this.businessId = apiCreateRankList.getBusinessId();
        this.name = apiCreateRankList.getName();
        this.description = apiCreateRankList.getDescription();
        this.isPublic = apiCreateRankList.isPublic();
        this.updateTime = new Timestamp(System.currentTimeMillis());
        this.sqlQuery = apiCreateRankList.getSql();
        this.type = RankListType.valueOf(apiCreateRankList.getType());
        //todo viewName策略
        this.viewName = getViewName(apiCreateRankList, ownerEmail);
        this.weight = apiCreateRankList.getWeight();
    }

    private String getViewName(ApiCreateRankList apiCreateRankList, String email){
        return apiCreateRankList.getType() + '_' + ServiceUtil.getCleanEmail(email) + '_' + apiCreateRankList.getName();
    }

    public BusinessUser getBusinessUser() {
        return businessUser;
    }

    public SqlPersistence getSqlQuery() {
        return sqlQuery;
    }

    public void setSqlQuery(SqlPersistence sqlQuery) {
        this.sqlQuery = sqlQuery;
    }

    public void setBusinessUser(BusinessUser businessUser) {
        this.businessUser = businessUser;
    }

    public boolean isPublic() {
        return isPublic;
    }

    public void setPublic(boolean isPublic) {
        this.isPublic = isPublic;
    }

    public RankListType getType() {
        return type;
    }

    public void setType(RankListType type) {
        this.type = type;
    }

    private byte[] serializeSql(SqlPersistence sql){
        return SerializationUtils.serialize(sql);
    }

    private SqlPersistence deSerializeSql(byte[] sql){
        return SerializationUtils.deserialize(sql);
    }

    public String getBusinessId() {
        return businessId;
    }

    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getIsPublic() {
        return isPublic;
    }

    public void setIsPublic(Boolean isPublic) {
        this.isPublic = isPublic;
    }

    public String getViewName() {
        return viewName;
    }

    public void setViewName(String viewName) {
        this.viewName = viewName;
    }

    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

}
